$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'psparamset/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '参数名称', name: 'paraName', index: 'para_name', width: 80 }, 			
			{ label: '参数标识', name: 'paraIdentifier', index: 'para_identifier', width: 80 }, 			
			{ label: '参数值', name: 'paraValue', index: 'para_value', width: 80,
				formatter:function (v) {
					return v*100+"%";
           		 }
            },
			{ label: '参数组别', name: 'paraGroup', index: 'para_group', width: 80  ,hidden:true}
        ],
		viewrecords: true,
        height: 700,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		psParamSet: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.psParamSet = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
            var paraName = $("#paraName").val();
            if(paraName != null){
                $("#paraName").attr("readonly","readonly");
            }
            var paraIdentifier = $("#paraIdentifier").val();
            if(paraIdentifier != null){
                $("#paraIdentifier").attr("readonly","readonly");
            }
		},
		saveOrUpdate: function (event) {
			if ($("#paraValue").val() == '' || $("#paraValue").val() == null ) {
				alert("百分比值不能为空!");
				return ;
			}
			var url = vm.psParamSet.id == null ? "psparamset/save" : "psparamset/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.psParamSet),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "psparamset/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "psparamset/info/"+id, function(r){
                vm.psParamSet = r.psParamSet;
                // vm.psParamSet.paraValue = vm.psParamSet.paraValue*100;
                $("#paraValue").val(vm.psParamSet.paraValue*100);
                // document.getElementById("paraValue").value = vm.psParamSet.paraValue*100;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		testValue:function () {
            var reg = /^((?!0)\d{1,2}|99)$/;
            var num = $("#paraValue").val();
            if(!num.match(reg)){
                $("#paraValue").val('');
                // vm.psParamSet.paraValue = '';
            } else {
                vm.psParamSet.paraValue = num/100;
			}
        }
	}

});